package com.auxgroup.smarthome.controller;

import com.auxgroup.smarthome.basebean.MenuTypeEnum;
import com.auxgroup.smarthome.dto.SearchUserDto;
import com.auxgroup.smarthome.dto.UpdatePersonCenterDto;
import com.auxgroup.smarthome.dto.UpdateUserDto;
import com.auxgroup.smarthome.dto.UserDto;
import com.auxgroup.smarthome.entity.UserEntity;
import com.auxgroup.smarthome.entity.UserStatus;
import com.auxgroup.smarthome.filter.annotation.PermissionMenu;
import com.auxgroup.smarthome.service.UserService;
import com.auxgroup.smarthome.vo.UserListVo;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * 系统用户管理
 * Created by zonghua on 2017/1/2.
 */
@Api(tags = "系统管理")
@RestController
@RequestMapping(value = "/upms")
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录用户名是否存在",notes = "查询登录用户名是否重名-创建与修改用户信息的时候前端异步请求")
    @GetMapping(value = "/users/exists_username")
    @PermissionMenu(id = "upms_user_button_isRepeat", parentId = "umps_menu_user_manage", name = "用户名重复", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse isExistRepeateName(@RequestParam @NotEmpty(message = "token不能为空") String userName){
        return userService.isExistUsername(userName);
    }

    @ApiOperation(value = "创建用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户账号（登录账号）", required = true, paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "password", value = "密码（明文）长度不小于6位",required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "roleId", value = "角色id",  required = true,  paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "realName", value = "昵称", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "phone", value = "手机号码", paramType = "form", dataType = "string")
    })
    @PostMapping(value = "/user")
    @PermissionMenu(id = "upms_user_button_create", parentId = "umps_menu_user_manage", name = "创建用户", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse create(@ApiIgnore @Valid UserDto userDto) {
        return userService.createUser(userDto);
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping(value = "/users/{userId}")
    @PermissionMenu(id = "upms_user_button_delete", parentId = "umps_menu_user_manage", name = "删除用户", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse delete(@PathVariable("userId") String userId) {
        return userService.deleteUserByIds(userId);
    }

    @ApiOperation(value = "批量删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIds", value = "多个用户id用英文，隔开", required = true, paramType = "form", dataType = "string"),
    })
    @DeleteMapping(value = "/users")
    @PermissionMenu(id = "upms_user_button_batch_delete", parentId = "umps_menu_user_manage", name = "批量删除用户", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse batchDelete(@RequestParam("userIds") String userIds) {
        return userService.deleteUserByIds(userIds);
    }

    @ApiOperation(value = "更新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id",  required = true,  paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码（明文）", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "realName", value = "昵称", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "phone", value = "手机号码", paramType = "form", dataType = "string")
    })
    @PutMapping(value = "/users/{userId}")
    @PermissionMenu(id = "upms_user_button_update", parentId = "umps_menu_user_manage", name = "更新用户", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse update(@PathVariable("userId") String userId, @Valid @ApiIgnore UpdateUserDto userDto) {
        return userService.updateUser(userId, userDto);
    }

    @ApiOperation(value = "更新个人中心资料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "realName", value = "真实姓名",  required = true,  paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "phone", value = "手机号码", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "email", value = "邮箱", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "headImg", value = "头像", paramType = "form", dataType = "string")
    })
    @PutMapping(value = "/users/person-center")
    @PermissionMenu(id = "upms_user_button_update_person_center", parentId = "umps_menu_user_manage", name = "更新个人中心资料", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse updatePersonCenter(@Valid @ApiIgnore UpdatePersonCenterDto personCenterDto) {
        return userService.updatePersonCenter(personCenterDto);
    }

    @ApiOperation(value = "修改个人密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "newPassword", value = "新密码", paramType = "form", dataType = "string", required = true),
    })
    @PutMapping(value = "/users/modify-password")
    @PermissionMenu(id = "upms_user_button_modify_password", parentId = "umps_menu_user_manage", name = "修改个人密码", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse modifyPassword(@NotEmpty(message = "旧密码不能为空") String oldPassword, @NotEmpty(message = "新密码不能为空") String newPassword) {
        return userService.modifyPassword(oldPassword, newPassword);
    }

    @ApiOperation(value = "启用/禁用用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户uid", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "status", value = "用户状态 NORMAL：正常 DISABLED：禁用", required = true, paramType = "form", dataType = "int"),
    })
    @PutMapping(value = "/user/status")
    @PermissionMenu(id = "upms_user_button_enable", parentId = "umps_menu_user_manage", name = "启用/禁用用户", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse ChangeUserStatus(@RequestParam("userId") String userId, @RequestParam("status") UserStatus status) {
        return userService.changeUserStatus(userId,status);
    }


    @ApiOperation(value = "搜索用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页号", paramType = "query", dataType = "int", defaultValue = "0"),
            @ApiImplicitParam(name = "size", value = "页的大小", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "createTimeSlot", value = "创建时间段:2017-07-01~2017-08-01", paramType = "query", dataType = "string")
    })
    @GetMapping(value = "/users")
    @PermissionMenu(id = "upms_user_button_search", parentId = "umps_menu_user_manage", name = "用户列表", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse<Page<UserListVo>> searchUsers(@ApiIgnore Pageable pageable, @ApiIgnore SearchUserDto searchUserDto) {
        Page<UserListVo> userListVoPage = userService.searchUsers(searchUserDto, pageable);
        return ApiResponse.ok(userListVoPage);
    }


    @ApiOperation(value = "查看用户详情")
    @GetMapping(value = "/users/{userId}")
    @PermissionMenu(id = "upms_user_button_showone", parentId = "umps_menu_user_manage", name = "查看用户详情", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse<UserListVo> getUserById(@PathVariable("userId") String userId) {
        UserEntity userEntity = userService.getUserById(userId);
        return ApiResponse.ok(new UserListVo(userEntity));
    }


}